package com.eboss2.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.myapplication.MyApplication;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyWalletBalanceActivity extends BaseActivity {


    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_amt)
    TextView tvAmt;
    @BindView(R.id.tv_dj_amt)
    TextView tvDjAmt;
    @BindView(R.id.tv_js_amt)
    TextView tvJsAmt;
    @BindView(R.id.rl_tx)
    RelativeLayout rlTx;
    @BindView(R.id.rl_settle)
    RelativeLayout rlSettle;
    @BindView(R.id.next_sure)
    TextView nextSure;

    private int balanceAmt;
    private int feeBalance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_mywallet);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();
    }

    private void initView() {
        RelativeLayout relativeLayout = relativeLayout(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "钱包余额");
        showView(R.id.next_sure, true);
        setTvText(R.id.next_sure, "提现记录");

//		tv_amt.setText(StringUtils.addAmtComma(""));


    }

    @Override
    protected void onResume() {
        super.onResume();
        getdata();
    }

    private void getdata() {
        showProgress("加载中...");

        String url = Config.SBS_QB_BALANCE;
        RequestParams params = new RequestParams();
        params.put("shopId", MyApplication.getmInstance().getLoginMEntity().getShopid() + "");

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @SuppressWarnings("unchecked")
            @Override
            public void success(String response) {
                disShowProgress();
                try {


                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }

                    JSONObject obj = result.getJSONObject("data");

                    balanceAmt = obj.optInt("balance");
                    feeBalance = obj.optInt("feeBalance");
                    String balance = StringUtils.formatIntMoney(balanceAmt);
                    String realAmount = StringUtils.formatIntMoney(obj.optInt("realAmount"));
                    String lockBalance = StringUtils.formatIntMoney(obj.optInt("lockBalance"));
                    String fee = StringUtils.formatIntMoney(feeBalance);
                    String gmtCreate = obj.optString("gmtCreate");

                    tvAmt.setText(balance);
                    tvDjAmt.setText(fee);
                    tvJsAmt.setText(lockBalance);


                } catch (Exception e) {
                    e.getMessage();

                }
            }

            @Override
            public void failed(Throwable error) {
                disShowProgress();
                showE404();
            }
        });
    }


    @OnClick({R.id.titleback_btn_back, R.id.rl_tx, R.id.rl_settle, R.id.next_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.rl_tx:
                if (balanceAmt == 0) {
                    ToastUtils.showShort(mContext, "暂无可提现金额");
                    return;
                }
                if (feeBalance != 0) {
                    ToastUtils.showShort(mContext, "您有一笔待处理交易");
                    return;
                }
                startActivity(new Intent(mContext, MyTxActivity.class).putExtra("balance", balanceAmt));
                break;
            case R.id.rl_settle:
                startActivity(new Intent(mContext, MySettleActivity.class));
                break;
            case R.id.next_sure:
                startActivity(new Intent(mContext, TxRecordActivity.class));
                break;
        }
    }


}
